﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PYS.Session;

namespace PYS.Controllers
{
    public class VoteController : Controller
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id1">CourseID</param>
        /// <param name="id2">ChoregraphyTypeID</param>
        /// <returns></returns>
        public ActionResult Vote(int id1, int id2)
        {
            Session["CourseID"] = id1;
            return View(GetModel(id2));
        }

        public ActionResult Confirm()
        {
            List<VoteWorkflow> workFlow = GetVoteResult();
            return View(workFlow);
        }

        [HttpPost]
        public ActionResult Confirm(List<VoteWorkflow> model)
        {
            PYSEntities entities = new PYSEntities();
            List<VoteWorkflow> workFlow = GetVoteResult();
            foreach (VoteWorkflow voteInWorkFlow in workFlow)
            {
                var choregraphy = entities.VoteWorkflows.FirstOrDefault(vwf=>vwf.mixID == voteInWorkFlow.mixID && vwf.chTypeID == voteInWorkFlow.chTypeID);
                if (choregraphy != null)
                {
                    int choregraphyID = choregraphy.chID;
                    int courseID = (int)Session["CourseID"];
                    PYS.vote vote = entities.votes.FirstOrDefault(v => v.chID == choregraphyID && v.courseID == courseID);
                    if (vote == null)
                    {
                        vote = new PYS.vote();
                        vote.chID = choregraphyID;
                        vote.courseID = (int)Session["CourseID"];
                        entities.votes.AddObject(vote);
                    }
                    else
                    {
                        vote.vCount++;
                    }
                }
            }
            PYS.voteUser voteUser = new PYS.voteUser();
            voteUser.userID = User.Identity.Name;
            voteUser.courseID = (int)Session["CourseId"];
            entities.voteUsers.AddObject(voteUser);
            entities.SaveChanges();

     //       SessionAnswers.ClearAnswers(Session);

            return View();
        }

        private List<VoteWorkflow> GetVoteResult()
        {
            PYSEntities entities = new PYSEntities();
            var chTypes = (from v in entities.VoteWorkflows
                           select new { v.chName, v.chTypeID }).Distinct();


            List<VoteWorkflow> workflow = new List<VoteWorkflow>();

            foreach (var chType in chTypes)
            {
                VoteWorkflow vote = new VoteWorkflow();
                vote.chName = chType.chName;
                vote.chTypeID = chType.chTypeID;
                int mixID = SessionAnswers.GetAnswer(Session, chType.chTypeID);
                mix selectedMix = entities.mixes.FirstOrDefault(m => m.mixID == mixID);
                if (selectedMix != null)
                {
                    vote.mixName = selectedMix.name;
                    vote.mixID = selectedMix.mixID;
                }
                workflow.Add(vote);
            }

            return workflow;
        }

        [HttpPost]
        public ActionResult Vote(ChoregraphyMix model, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                bool confirm = collection["confirm"] != null;
                bool forward = collection["fw"] != null;
                if (model.SelectedAnswer != 0)
                {
                    SessionAnswers.AddAnswer(Session, model.ChoregraphyID, model.SelectedAnswer);
                }
                int nextChoregraphyID = model.ChoregraphyID + (forward ? 1 : -1);
                if (nextChoregraphyID > 11 || confirm)
                {
                    return RedirectToAction("Confirm");
                }
                else
                {
                    return RedirectToAction("Vote", new { id1 = (int)Session["CourseID"], id2 = nextChoregraphyID, id3="" });
                }
            }
            return View();
        }


        private ChoregraphyMix GetModel(int choregraphyID)
        {
            PYS.PYSEntities context = new PYS.PYSEntities();
            var workflow = context.VoteWorkflows.Where(x => x.chTypeID == choregraphyID);
            var choregraphy = workflow.ToList<VoteWorkflow>()[0];

            var cmix = new ChoregraphyMix { 
                ChoregraphyID = choregraphy.chID, 
                ChoregraphyName = choregraphy.chName, 
                Workflow = workflow.ToList(),
                SelectedAnswer = SessionAnswers.GetAnswer(Session, choregraphyID)
            };
            
            return cmix;
        }

    }
}
